//
//  UserSetController.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/5/30.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit
import RxSwift
import SwiftyJSON


class UserSetController: CBBaseViewController {
    
    //头像
    @IBOutlet weak var ivAvatar: UIImageView!
    //手机号码
    @IBOutlet weak var lblMobile: UILabel!
    //地址
    @IBOutlet weak var lblAddress: UILabel!
    //身份证号
    @IBOutlet weak var lblIdcard: UILabel!
    //退出按钮
    @IBOutlet weak var btnExit: UIButton!

    weak var updateUserInfo:Variable<Bool>?
    weak var userModel:Variable<UserModel?>?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "用户设置"

        btnExit.rx.tap
            .asObservable().flatMapLatest { _ -> Observable<Bool> in
                SVProgressHUD.show()
                return NetworkTool.requestData(url: ActionURL.LOGOUT, param: nil)
                    .map({ json -> Bool in
                        return true
                    }).catchErrorJustReturn(false)
            }
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] isLogout  in
                SVProgressHUD.dismiss()
                if isLogout {
                    MainTools.shared.userModel = nil
                    self?.navigationController?.tabBarController?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
                    self?.navigationController?.tabBarController?.present((UIStoryboard(name: "LoginAndRegister", bundle: nil).instantiateInitialViewController() as? UINavigationController) ?? UINavigationController(), animated: true, completion:nil)
                    //回退
                    self?.navigationController?.popToRootViewController(animated: false)
                }
            })
            .disposed(by: disposeBag)
        
        userModel?.asObservable()
            .subscribe(onNext: { [weak self] _ in
                self?.flushView()
            }).disposed(by: disposeBag)
    }
    
    func flushView() {
        if let userModel = MainTools.shared.userModel {
            ivAvatar.image = UIImage(named: (userModel.img.isEmpty || userModel.img == "0") ? "null_avtar" : userModel.img)
            lblMobile.text = userModel.mobile
            lblAddress.text = userModel.address
            lblIdcard.text = userModel.idcard
        }
    }
    
}

//跳转事件
extension UserSetController {
    
    //打开头像选择
    @IBAction func openMyAvatar(_ sender: UITapGestureRecognizer) {

    }
    
    //跳转到密码修改
    @IBAction func pushToChangePwd(_ sender: UITapGestureRecognizer) {
        let vc = UIStoryboard(name: "LoginAndRegister", bundle: nil).instantiateViewController(withIdentifier: "recoverPasswordController") as? RecoverPasswordController
        self.navigationController?.pushViewController(vc!, animated: true)
    }
    
    //跳转到地址管理
    @IBAction func pushToAddressManage(_ sender: UITapGestureRecognizer) {
        let vc = UIStoryboard(name: "My", bundle: nil).instantiateViewController(withIdentifier: "addressManageController") as? AddressManageController
        vc?.userModel = userModel?.value
        vc?.updateUserInfo = updateUserInfo
        self.navigationController?.pushViewController(vc!, animated: true)
    }
    
    //跳转到身份认证
    @IBAction func pushToIdcard(_ sender: UITapGestureRecognizer) {
        let vc = MainTools.initSbVc(sbName: "My", AnyClass: IdCardController.self)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}
